package com.bilin.coin.activity;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.ApiConstant;
import com.bilin.coin.R;
import com.bilin.coin.bean.BiLinAmount;
import com.bilin.coin.bean.CommonSuccessEvent;
import com.bilin.coin.common.base.UploadImgActivity;
import com.bilin.coin.tools.Geter;
import com.bilin.coin.tools.Poster;
import com.bilin.coin.utils.GlideUtils;
import com.bilin.coin.utils.GsonUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


@Route(path = "/safe/AuthActivity")
public class AuthActivity extends UploadImgActivity {

    @BindView(R.id.iv_front)
    ImageView iv_front;
    @BindView(R.id.iv_backend)
    ImageView iv_backend;
    @BindView(R.id.iv_avatar)
    ImageView iv_avatar;
    @BindView(R.id.tv_user_name)
    TextView tv_user_name;
    @BindView(R.id.tv_user_id)
    TextView tv_user_id;
    @BindView(R.id.et_user_name)
    TextView et_user_name;
    @BindView(R.id.et_user_id)
    TextView et_user_id;
    @BindView(R.id.tv11)
    TextView tv11;
    @BindView(R.id.tv22)
    TextView tv22;



    @BindView(R.id.cb_invite)
    CheckBox cb_invite;
    @BindView(R.id.cb_amount)
    CheckBox cb_amount;

    @BindView(R.id.ll3)
    LinearLayout ll3;
    @BindView(R.id.ll2)
    LinearLayout ll2;

    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.rl_invite)
    RelativeLayout rl_invite;



    int type; //0上传正面 1上传反面 2手持

    String typeWay = "0";//支付比邻券的类型 0-自己支付 1-上级代付


    @Override
    protected void initView() {
        setTitle("实名认证");
        initEvent();
        getDataBiLin();
    }

    private void getDataBiLin() {
        new Geter(this, false, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-wallet/getBlq";
            }

            @Override
            protected Map<String, String> fillParams() {
                return new HashMap<>();
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                BiLinAmount biLinAmount = GsonUtil.GsonToBean(dataJson, BiLinAmount.class);
                tv11.setText("可用比邻券数量: "+biLinAmount.getSelfAmount());
                tv22.setText("邀请人比邻券数量: "+biLinAmount.getParentAmount());
                rl_invite.setVisibility(biLinAmount.isOpen()?View.VISIBLE:View.GONE);
            }
        };
    }

    private void initEvent() {
        cb_amount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (typeWay != "0") {
                    typeWay = "0";
                    cb_invite.setChecked(false);
                } else {
                    cb_amount.setChecked(true);
                }
            }
        });
        cb_invite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (typeWay != "1") {
                    typeWay = "1";
                    cb_amount.setChecked(false);
                } else {
                    cb_invite.setChecked(true);
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected String fillUrl() {
        return "/api/user/upload";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_auth;
    }

    @OnClick({R.id.ll_front,R.id.ll_backend,R.id.ll_avatar,R.id.bt_confirm})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.ll_front:
                type = 0;
                popupWindow.showAtLocation(R.layout.activity_auth, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.ll_backend:
                type = 1;
                popupWindow.showAtLocation(R.layout.activity_auth, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.ll_avatar:
                type = 2;
                popupWindow.showAtLocation(R.layout.activity_auth, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.bt_confirm:
                auth();
                break;
        }
    }

    private void auth() {
        if (TextUtils.isEmpty(idCardFront)) {
            showToast("请上传身份证正面");
            return;
        }
        if (TextUtils.isEmpty(idCardBackend)) {
            showToast("请上传身份证背面");
            return;
        }
        if (TextUtils.isEmpty(identityImg)) {
            showToast("请上传人像照");
            return;
        }
        String name = et_user_name.getText().toString().trim();
        String idCardNo = et_user_id.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            showToast("姓名不能为空");
            return;
        }
        if (TextUtils.isEmpty(idCardNo)) {
            showToast("身份证号不能为空");
            return;
        }
        new Poster(this, true,true) {
            @Override
            protected String fillUrl() {
                return "/api/user/kycAll";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("idCardBackend",idCardBackend);
                hashMap.put("idCardFront",idCardFront);
                hashMap.put("identityImg",identityImg);
                hashMap.put("idCardNo",idCardNo);
                hashMap.put("name",name);
                hashMap.put("type",typeWay);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                EventBus.getDefault().post(new CommonSuccessEvent(CommonSuccessEvent.AuthSuccess));
                finish();
            }
        };
    }

    String idCardBackend = "";
    String idCardFront = "";
    String identityImg  = "";

    @Override
    protected void childDoWork(String backImgUrl) {
        super.childDoWork(backImgUrl);
        if (TextUtils.isEmpty(backImgUrl)) {
            showToast("上传失败");
            return;
        }
        if (type == 0) {
            idCardFront = backImgUrl;
            iv_front.setVisibility(View.VISIBLE);
            GlideUtils.getInstance().loadRoundImageCard(this, ApiConstant.OSSURL+backImgUrl, iv_front, 2);
            //ll1.setVisibility(View.GONE);
        } else if (type == 1){
            idCardBackend = backImgUrl;
            iv_backend.setVisibility(View.VISIBLE);
            GlideUtils.getInstance().loadRoundImageCard(this, ApiConstant.OSSURL+backImgUrl, iv_backend, 2);
            //ll2.setVisibility(View.GONE);
        } else {
            identityImg = backImgUrl;
            iv_avatar.setVisibility(View.VISIBLE);
            GlideUtils.getInstance().loadRoundImageCard(this, ApiConstant.OSSURL+backImgUrl, iv_avatar, 2);
            //ll3.setVisibility(View.GONE);
        }
    }
}
